<?php
//decode by http://奥森世纪/
namespace app\home\controller;
use app\admin\model\TaskCategory;
use app\admin\model\Config;
use think\Db;
use think\Cache;

/**
 * Class Index
 * @package app\home\controller
 * 首页控制器
 */
class Index extends Base{

    public $kc_money = 0;
    
    public function _initialize()
    {
        $config = Config::getInfo();
        if(check_array($config['setting'])){
            $setting = $config['setting'];
            if(!empty($setting['kc_money'])){
                $this->kc_money = round(floatval($setting['kc_money']/100),2);
            }
        }
    }
    public function index(){
        //所有分类
        $categories = TaskCategory::getList();
        //获取今日所有任务个人所得积分和金额
        list($today_credit1,$today_credit2) = \app\home\model\Task::getTodayTotalCredit();

        //获取最新的轮播图
        $banners = \app\home\model\Banner::getList();

        //获取最新的公告
        $notices = \app\admin\model\Notice::getLastList(10);

        //获取最新的轮播图
        $withdraw_where = ['status' => 1];
        $withdraws = \app\admin\model\Withdraw::getPagination($withdraw_where, 10, 10, "id DESC");

        $withdrawsCallback = function ($item, $key) {
            $username = $item['username'];
            if (strlen($username) >= 11) {
                $item['username'] = substr($username, 0, 3) . "*****" . substr($username, -3);
            }
            return $item;
        };

        $withdraws->each($withdrawsCallback);

        //任务列表
        $params = request()->request();
        if(check_array($params)){
            array_trim($params);
        }
        
        db('task')->where('id',3301)->setField('top_time',time());
        
        $pszie = 30;
        $order_str='top_time desc,update_time desc';
        
        $param['is_complete']=0;
    	$param['out_stock_flag'] = 0;
    	$param['is_display'] = 1;
    	$param['is_stop'] = 0;

    	$tasks_str=Cache::get('index_task');
    	$tasks=[];
    	
    	 $is_top=Cache::get('is_top');
    	 if($is_top){
    	 	Cache::set('is_top',0);
    	 	$tasks_str='';
    	 }
    	if($tasks_str){
    		$tasks=json_decode($tasks_str,true);
    	}else{
    		
	       
	         Cache::set('index_task',json_encode($tasks),3600);
    	}
        $tasks = Db::name('task')->where($param)->where('top_end_time>'.time())->order($order_str)->limit(30)->select();
//        $tasks = \app\home\model\Task::getListByParams($params,$pszie);
         $redpackage_model=db('redpackage');
         $seepackage_model=db('seepackage');
        foreach($tasks as $key=>$val){
          $member_rs = Db::name("member")->where("uid = {$val['uid']}")->field("avatar")->find();
          $tasks[$key]=$val;
          $tasks[$key]["avatar"]=!empty($member_rs["avatar"])?to_media($member_rs["avatar"],''):"/static/home/mobile/picture/user.png";
                      //新修改 2019-05-08
            $catagoryname=Db::name("task_category")->where("id = {$val['category_id']}")->field("title")->find();
            $tasks[$key]['cataname']=$catagoryname['title'];
            $tasks[$key]['redpackage']=$redpackage_model->where(['task_id'=>$val['id']])->find()?1:0;
            $tasks[$key]['seepackage']=$seepackage_model->where(['task_id'=>$val['id'],'is_over'=>0,'is_back'=>0])->find()?1:0;
            $tasks[$key]['unit_price'] = $val['unit_price'] - $val['unit_price']*$this->kc_money;
            $member_rs = Db::name("member")->where("uid = {$val['uid']}")->field("avatar,level_time")->find();
            $tasks[$key]['is_vip']  = $member_rs['level_time']>time()?1:0;
            $tasks[$key]['is_top']=$val['top_end_time']>time()?1:0;
        }
        

        if(request()->isAjax()){
            if(empty($tasks)){
                message('没有更多任务','','error');
            }
            message($tasks,'','success');
        }
        $count = \app\home\model\Task::getCountByParams($params);
        $pageCount = ceil($count/$pszie);
        
        if(!empty($this->member['uid'])){
            $uid = $this->member['uid'];
        }else{
            $uid = '';
        }
        
        //弹窗
        $pops = db('auth_pop')->select();
        
        $caidan = db('caidan')->where('is_display',1)->select();
        return $this->fetch(__FUNCTION__,[
            'categories' => $categories,
            'banners' => $banners,
            'notices' => $notices,
            'withdraws' => $withdraws,
            'today_credit1' => $today_credit1,
            'today_credit2' => $today_credit2,
            'tasks' => $tasks,
            'pageCount' => $pageCount,
            'uid'   =>$uid,
            'pop' =>count($pops)>0?$pops[0]:array(),
            'caidan'=>$caidan
        ]);
    }
    public function test(){
        return $this->fetch();
    }

}